#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define MAX_SIZE              1024
#define MAXLINE               1024
#define PAGER                "${PAGER:-more}"

int main(int argc, char *argv[])
{
    char line[MAX_SIZE];
    FILE *fpin, *fpout;

    if (argc != 2) {
        printf("usage: a.out <pathname>\n");
        exit (-1);
    }
    if ((fpin = fopen(argv[1], "r")) == NULL)
        printf("can't open %s\n", argv[1]);

    if ((fpout = popen(PAGER, "w")) == NULL)
        printf("popen error");

    while (fgets(line, MAXLINE, fpin) != NULL) {
        if (fputs(line, fpout) == EOF)
            perror("fputs error to pipe");
    }
    if (ferror(fpin))
        perror("fgets error");
    if (fclose(fpout) == -1)
        perror("fclose error");

    return 0;
}

